import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.sql.*;

@WebServlet("/AddCompanyServlet")
public class AddCompanyServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");

        response.sendRedirect("company_add.jsp");

        String companyName = request.getParameter("companyName");
        String location = request.getParameter("location");
        String size = request.getParameter("size");
        String nature = request.getParameter("nature");
        String description = request.getParameter("description");
        String status = request.getParameter("status");
        String displayOrder = request.getParameter("displayOrder");
        String promoImage = request.getParameter("promoImage");

        // 连接数据库
        String jdbcUrl = "jdbc:mysql://localhost:3306/jianggweb";
        String dbUser = "root";
        String dbPassword = "123456";

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection connection = DriverManager.getConnection(jdbcUrl, dbUser, dbPassword);
//          插入数据到企业表
            String sql = "INSERT INTO companies (companyName, location, size,nature,description,status, displayOrder, promoImage) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
            PreparedStatement statement = connection.prepareStatement(sql);
            statement.setString(1, companyName);
            statement.setString(2,location);
            statement.setString(3,size);
            statement.setString(4,nature);
            statement.setString(5,description);
            statement.setString(6,status);
            statement.setString(7,displayOrder);
            statement.setString(8,promoImage);

            statement.executeUpdate();
            connection.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}